//
//  ZZCEditAddressController.m
//  ZZC-Finance
//
//  Created by mac on 2017/5/24.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "ZZCEditAddressController.h"
#import "ZZCDeleteView.h"

@interface ZZCEditAddressController ()
@property (strong, nonatomic) CityChoose *cityChoose;
@property (strong, nonatomic) ZZCDeleteView *deleteBGView;
//收货人
@property (weak, nonatomic) IBOutlet UITextField *nameFLD;
//收货人电话
@property (weak, nonatomic) IBOutlet UITextField *phoneFLD;
//所在地区
@property (weak, nonatomic) IBOutlet UILabel *areaLabel;
//详细地址
@property (weak, nonatomic) IBOutlet UITextField *addressDetailsFLD;
//街道View
@property (weak, nonatomic) IBOutlet UIView *areaView;
//街道地址
@property (weak, nonatomic) IBOutlet UIView *streetView;
//删除地址view
@property (weak, nonatomic) IBOutlet UIView *deleteView;

@property (copy, nonatomic) NSString *province;
@property (copy, nonatomic) NSString *city;
@property (copy, nonatomic) NSString *area;


@end

@implementation ZZCEditAddressController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"修改地址";
    self.view.backgroundColor = ZZCBGColor;
    [self addGesturesToControls];
    self.nameFLD.text = self.name;
    self.phoneFLD.text = self.phone;
    self.areaLabel.text = self.areaText;
    self.addressDetailsFLD.text = self.address;
    NSString *str1 = self.areaText;
    
    NSRange range = [str1 rangeOfString:@"-"];//匹配得到的下标
    NSLog(@"rang:%@",NSStringFromRange(range));
    self.province = [str1 substringToIndex:range.location];//截取掉下标7之后的字符串
    NSString *str2 = [str1 substringFromIndex:range.location+1];//截取掉下标2之前的字符串
    NSRange range1 = [str2 rangeOfString:@"-"];
    self.city = [str2 substringToIndex:range1.location];
    NSLog(@"%@",self.city);
    self.area = [str2 substringFromIndex:range1.location+1];
    NSLog(@"%@",self.area);
    
}

//给控件添加手势
- (void)addGesturesToControls {
    self.areaView.userInteractionEnabled = YES;
    UITapGestureRecognizer *areaTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(areaTapClick)];
    [self.areaView addGestureRecognizer:areaTap];
    self.streetView.userInteractionEnabled = YES;
    UITapGestureRecognizer *streetTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(streetTapClick)];
    [self.streetView addGestureRecognizer:streetTap];
    self.deleteView.userInteractionEnabled = YES;
    UITapGestureRecognizer *deleteTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(deleteTapClick)];
    [self.deleteView addGestureRecognizer:deleteTap];
}
//监听控件点击
- (void)areaTapClick {
    ZZCLog(@"你点击了地区");
    self.cityChoose = [[CityChoose alloc] init];
    __weak typeof(self) weakSelf = self;
    self.cityChoose.config = ^(NSString *province, NSString *city, NSString *town){
        weakSelf.areaLabel.text = [NSString stringWithFormat:@"%@-%@-%@",province,city,town];
        weakSelf.province = province;
        weakSelf.city = city;
        weakSelf.area = town;
    };
    [self.view addSubview:self.cityChoose];
}
- (void)streetTapClick {
    ZZCLog(@"你点击了街道");
//    ZZCTempController *Temp = [ZZCTempController new];
//    Temp.title = @"街道";
//    [self.navigationController pushViewController:Temp animated:YES];
}
- (void)deleteTapClick {
    ZZCLog(@"你点击了删除");
    [self.view addSubview:self.deleteBGView];
    [self.deleteBGView.deleteBtn addTarget:self action:@selector(deleteBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.deleteBGView.cancelBtn addTarget:self action:@selector(cancelBtnClick) forControlEvents:UIControlEventTouchUpInside];
}

//保存
- (IBAction)preserveClick {
    if ([_areaLabel.text isEqualToString:@"请选择"]) {
        [SVProgressHUD showInfoWithStatus:@"请选择地区"];
        return;
    }


    NSString *token = ZZCNSUserDefaults(@"token");
    NSDictionary *dict = @{@"province":self.province,
                           @"city":self.city,
                           @"area":self.area,
                           @"street":self.addressDetailsFLD.text,
                           @"receiver":self.nameFLD.text,
                           @"tel":self.phoneFLD.text,
                           @"id":self.id,
                           @"token":token
                           };
    NSString *sign = [ZZCRequest getSignWithDict:dict];
    sign = [NSString md5:[sign lowercaseString]];
    NSDictionary *param = @{@"timestamp":[NSString timeStr],@"sign":sign,
                            @"province":self.province,
                            @"city":self.city,
                            @"area":self.area,
                            @"street":self.addressDetailsFLD.text,
                            @"receiver":self.nameFLD.text,
                            @"tel":self.phoneFLD.text,
                            @"id":self.id,
                            @"token":token
                            };
    [[ZZCRequest sharedNetworkTools] POST:ZZCURL_API_UPDATEADDRESS parameters:param finished:^(id  _Nullable responseObject, NSError * _Nullable error) {
        if (error) {
            ZZCLog(@"失败-%@",error);
        }else {
            ZZCLog(@"-%@",responseObject);
//            [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
            if ([responseObject[@"code"] integerValue] == 400) {
                [UIAlertController showInfoAlertLoginVC:self title:@"提示" message:responseObject[@"msg"] ok:nil];
            }else {
                [self.navigationController popViewControllerAnimated:YES];
                [[NSNotificationCenter defaultCenter] postNotificationName:@"addressUpdate" object:self];
            }
        }
    }];

}

//删除
- (void)deleteBtnClick {
    ZZCLog(@"删除-");
    [self.deleteBGView removeFromSuperview];
    [self requestDleAddAreaData];
    [self.navigationController popViewControllerAnimated:YES];
}
//取消
- (void)cancelBtnClick {
    [self.deleteBGView removeFromSuperview];
}
- (void)requestDleAddAreaData {
    NSString *token = ZZCNSUserDefaults(@"token");
    NSDictionary *dict = @{@"id":self.id,@"token":token};
    NSString *sign = [ZZCRequest getSignWithDict:dict];
    sign = [NSString md5:[sign lowercaseString]];
    NSDictionary *param = @{@"timestamp":[NSString timeStr],@"sign":sign,@"id":self.id,@"token":token};
    [[ZZCRequest sharedNetworkTools] POST:ZZCURL_API_DELADDRESS parameters:param finished:^(id  _Nullable responseObject, NSError * _Nullable error) {
        if (error) {
            ZZCLog(@"失败-%@",error);
        }else {
            ZZCLog(@"-%@",responseObject);
            [self.navigationController popViewControllerAnimated:YES];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"addressUpdate" object:self];
            [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];

        }
    }];

}
- (ZZCDeleteView *)deleteBGView {
    if (_deleteBGView == nil) {
        _deleteBGView = [ZZCDeleteView deleteView];
        _deleteBGView.frame = CGRectMake(0, 0, ZZCScreenW, ZZCScreenH);
        _deleteBGView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
    }
    return _deleteBGView;
}
@end
